<template>
    <div>
        <input v-show="false" ref="input" type="file" @change="handleChange">
        <div @click="clickUp" class="uploadBtn">
            <img class="image" :src="imageUrl" alt="">
        </div>
        <!-- <div v-else class="uploadBtn">
            <img class="image" :src="imageUrl" alt="">
        </div> -->
    </div>
</template>

<script>
// import axios from 'axios'

export default {
    name: 'upload',
    props:{
        type: '',
        userOrg: ''
    },
    data(){
        return{
            imageUrl: '',
            Version: 0,
            Versionn: 0,
            canExt: [
                '.bmp',
                '.png',
                '.pdf',
                '.jpg',
                '.jpeg',
            ],
        }
    },
    methods:{
        handleChange(e){
            let file = e.target.files[0]

            if (!file) {
                return
            }
            let name = file.name,
            canExt = this.canExt,
            ext = name.substring(name.lastIndexOf('.'))
            if (canExt.indexOf(ext) == -1) {
                this.$Message.error('不支持此格式')
                this.$refs.input.value = null
                return
            }
            this.postFile(file)
        },

        clickUp(){
            if(this.userOrg == '12'|| this.userOrg == 'Ent'){
                this.$refs.input.click()
            }else{
                console.log('无权限上传')
            }
        },
        postFile(file){
            let self = this;
            var formdata = new FormData();
            formdata.append('IdentityType', this.type)
            formdata.append('Filedata',file)
            formdata.append('Version',this.type == '04'? this.Version:this.Versionn)
            // let config = {版本号不用传 他们后台生成的
            //     headers:{'Content-Type':'multipart/form-data'}
            // };http://api.ahjdev.top/upload/customer/company/businessLicense/765e6e79040b4cb7bcc15585eab53be4.jpg
            axios.post('/fileUpload/upload',formdata).then(res=>{
                this.$Message.success('上传成功')
                this.getImageUrl(self.type)
            })
        },
        getImageUrl(type){
            let self = this
            // console.log(this.type)
            this.$post('fileUpload/initCompanyCertificate',{}).then(res=>{
                // console.log(res)
                if(res.data04 && res.data05){
                    if(res.data04.IdentityType == type){
                        self.Version = res.data04.Version
                        self.imageUrl = res.data04.CoverFilePath
                    }else if(res.data05.IdentityType == type){
                        self.Versionn = res.data05.Version
                        self.imageUrl = res.data05.CoverFilePath
                    }
                }
            })
        },
    },
    created(){
        this.getImageUrl(this.type)
        if(this.type == '04'){
            this.imageUrl = 'https://ahj-storage-test.oss-cn-beijing.aliyuncs.com/pcfile/2019-1/a86981.png?Expires=33084302190&OSSAccessKeyId=LTAIBhKyqEQAbw7q&Signature=He9GfI%2FLa925qPUxVvGebShoqjc%3D';
        }else{
            this.imageUrl = 'https://ahj-storage-test.oss-cn-beijing.aliyuncs.com/pcfile/2019-1/039552.png?Expires=33084302245&OSSAccessKeyId=LTAIBhKyqEQAbw7q&Signature=ccF65OJj%2F%2FDT7ymdQx8wNP60dVs%3D'
        }
    }
}
</script>

<style lang="less">
.uploadBtn {
    width: 100%;
    height: 300px;
    cursor: pointer;
    border-radius: 5px;

    .image{
        width: 100%;
        height: 300px;
    }
}
</style>